package com.lagou.sqlSession;

import com.lagou.config.XMLConfigBuilder;
import com.lagou.pojo.Configuration;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * 用于创建SqlSessionFactory
 *  主要方法：SqlSessionFactory build(InputStream in)
 */
public class SqlSessionFactoryBuilder {
    /**
     * 构建SqlSessionFactory对象
     * @param in    配置文件字节输入流对象
     * @return
     */
    public SqlSessionFactory build(InputStream in) throws PropertyVetoException, DocumentException {
        // 1. 使用dom4j解析相关的配置文件并将解析出来的内容封装到核心配置类Configuration中
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
        Configuration configuration = xmlConfigBuilder.parseConfig(in);

        // 2.创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        return sqlSessionFactory;
    }
}
